import fs from "fs";
import path from "path";
import { copyDir, delDir } from "./utils/dir";

export class DistStruct {
  private rootPath: string;
  private resFolder: string;
  private cssFolder: string;
  private jsFolder: string;
  private imgFolder: string;
  private templatePath: string;
  private templateJsFolder: string;
  private templateImgFolder: string;
  private templateCssFolder: string;
  private templateResFolder: string;

  constructor(rootPath: string, templatePath: string) {
    this.rootPath = rootPath;
    this.templatePath = templatePath;

    this.resFolder = path.join(rootPath, "res");
    this.cssFolder = path.join(rootPath, "res/css");
    this.jsFolder = path.join(rootPath, "res/js");
    this.imgFolder = path.join(rootPath, "res/img");

    this.templateResFolder = path.join(templatePath, "res");
    this.templateJsFolder = path.join(templatePath, "res/js");
    this.templateCssFolder = path.join(templatePath, "res/css");
    this.templateImgFolder = path.join(templatePath, "res/img");
  }
  public getRootPath() {
    return this.rootPath;
  }

  public getResFolderPath() {
    return this.resFolder;
  }
  public getCssFolderPath() {
    return this.cssFolder;
  }
  public getJsFolderPath() {
    return this.jsFolder;
  }
  public getImgFolderPath() {
    return this.imgFolder;
  }

  public getTemplatePath() {
    return this.templatePath;
  }
  public getTemplateResFolderPath() {
    return this.templateResFolder;
  }
  public getTemplateCssFolderPath() {
    return this.templateCssFolder;
  }
  public getTemplateImgFolderPath() {
    return this.templateImgFolder;
  }
  public getTemplateJsFolderPath() {
    return this.templateJsFolder;
  }
}

export class CreateDist {
  private distPath: string;
  private templatePath: string;

  constructor(projectRoot: string, newFolderName: string, clean: boolean) {
    this.distPath = path.join(projectRoot, newFolderName);
    this.templatePath = path.join(projectRoot, "template");
    if (clean) {
      if (fs.existsSync(this.distPath)) delDir(this.distPath);
      fs.mkdirSync(this.distPath);
      copyDir(this.templatePath, this.distPath);
    }
  }

  public getDistStructure() {
    return new DistStruct(this.distPath, this.templatePath);
  }

  public reloadIndex() {
    fs.copyFileSync(
      path.join(this.templatePath, "/index.html"),
      path.join(this.distPath, "/index.html")
    );
  }
}
